<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['variety:cuisine:add']"
        >新增</el-button>
      </el-col>
    </el-row>
  <el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      prop="materialId"
      label="id"
      width="180">
    </el-table-column>
    <el-table-column
      prop="materialName"
      label="名称"
      width="180">
    </el-table-column>
    <el-table-column label="菜品图片" align="center" prop="materialImage" width="100">
      <template slot-scope="scope">
        <image-preview :src="scope.row.materialImage" :width="50" :height="50"/>
      </template>
    </el-table-column>
    <el-table-column
      prop="commercialId"
      label="商户id">
    </el-table-column>

    <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
        <el-button type="text" size="small" @click="handleAdd(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:18580/cuisine/oss"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

        <el-form-item label="原料名称">
          <el-input v-model="form.materialName"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
        </el-form-item>



      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {getAll,deleteById,save,updateCuisine} from "@/api/materials/material";
export default {
  name: "index",
  data(){
    return{
      tableData:[],
      dialogVisible:false,
      form:{
        materialImage:'',
        materialName:''
      },
      imageUrl:'',
      a:{
        commercialId:'',
        materialId:'',
        materialImage:'',
        materialName:''
      },
    }

  },
  methods:{
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.imageUrl = res;
      this.form.materialImage=res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      return ;
    },
    findPage(){
      getAll().then((resp)=>{
        console.log(resp)
        this.tableData=resp;
      })
    },
    handleAdd(row){
      this.dialogVisible=true;
      console.log(row);
      this.a.materialId=row.materialId;
      this.a.materialImage=row.materialImage;
      this.a.commercialId=row.commercialId;
      this.a.materialName=row.materialName;
    },
    handleClose(){
       this.dialogVisible=false;
    },
    handleClick(row){
      deleteById(row.materialId).then((resp)=>{
        this.$message.success(resp);
      })
    },
    onSubmit(){
       this.form.materialImage=this.imageUrl;
       if(this.a.materialId!=null){
         console.log(this.a);
           if(this.form.materialImage!=null && this.form.materialImage!=''){
             this.a.materialImage=this.form.materialImage;
           }
           if (this.form.materialName!=null && this.form.materialName!=''){
             this.a.materialName=this.form.materialName;
           }
           updateCuisine(this.a).then((resp)=>{
           this.$message.success(resp);
         })
       }else{
         save(this.form).then((resp)=>{
           this.$message.success(resp);
         })
       }
    }
  },
  created() {
    this.findPage();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
